/*!
* jquery.lately.js 0.1 - https://github.com/yckart/lately
* Lazy loaded images.
*
* Copyright (c) 2012 Yannick Albert (http://yckart.com)
* Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php).
* 2013/01/03
**/
;(function ($, window) {

  $.fn.lately = function (options) {

    options = $.extend({}, {
      container: window,
      gapX: 0,
      gapY: 0
    }, options);

    var $win = $(options.container),
      self = this;

    this.one("lately", function () {
      var src = this.getAttribute("data-src");
      this.setAttribute("src", src);
      this.removeAttribute("data-src");
    });

    function lately() {
      var inview = self.filter(function () {
        var el = $(this),
          elW = el.outerWidth() + options.gapX,
          elH = el.outerHeight() + options.gapY,

          scroll = {
            y: $win.scrollTop(),
            x: $win.scrollLeft()
          },

          viewport = {
            x: $win.width() + options.gapX,
            y: $win.height() + options.gapY
          };

        return (
        el.offset().top < (scroll.y + viewport.y) && el.offset().left < (scroll.x + viewport.x) && (el.offset().top + elH) > scroll.y && (el.offset().left + elW) > scroll.x);
      });

      var loaded = inview.trigger("lately");
      self = self.not(loaded);
    }

    $win.scroll(lately);
    $(window).resize(lately);
    lately();

    return this;
  };

})(jQuery, window);